package com.monboo.album.view {

	/**
	 * @author robin for Tudou AlbumPlayer 2010-03-26
	 * @AlbumMediator main Mediator
	 * @email:rman@live.cn
	 * @blog:www.xncat.com
	 */

	import com.monboo.album.command.AlbumEvent;
	import com.monboo.utils.FullState;
	
	import flash.display.Stage;
	import flash.display.StageDisplayState;
	import flash.events.Event;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;

	public class AlbumMediator extends Mediator{
		public static const NAME:String = "AlbumMediator"; 
		private var viewObj:AlbumLite;
		private var imageInfo:AlbumInfoMediator;
		private var imageAreaMediator:ImageAreaMediator;
		private var fullScreenMediator:FullScreenMediator;
		private var _stage:Stage;
		
		public function AlbumMediator(obj:Object){
			super(NAME, obj); 
			viewObj = obj as AlbumLite;
			
			_stage = viewObj.stage;
			_stage.addEventListener(Event.RESIZE, onStageResize);
			
			imageInfo = new AlbumInfoMediator(viewObj);
			facade.registerMediator(imageInfo);
			
			facade.registerMediator( new FullScreenMediator(viewObj) );
			
			imageAreaMediator =  new ImageAreaMediator(viewObj);
			facade.registerMediator(imageAreaMediator);
			
			trace("[AlbumInit Successfully!]");
		}
		
		override public function listNotificationInterests():Array 
        {  
            return [ 
            		AlbumEvent.SYN_AUTO_STATE, 
					AlbumEvent.FULL_SCREEN,
            		AlbumEvent.RESET_IMAGE
            		 ];
        }
		 override public function handleNotification( note:INotification ):void 
        {
            switch ( note.getName() ) 
            {  	
                case AlbumEvent.SYN_AUTO_STATE:
					if(imageInfo.gotoStop){
						viewObj.dispatchEvent(new Event(AlbumEvent.SYN_AUTO_STATE));
					}
					break;
				case AlbumEvent.FULL_SCREEN:
                	fullScreenFunc();
                	break;
                case AlbumEvent.RESET_IMAGE:
					imageInfo.text = imageAreaMediator.getText(imageInfo.current);
					viewObj.dispatchEvent(new Event(AlbumEvent.UPDATE_ALBUM));
                	break;
            }
        }
			
		public function fullScreenFunc():void{
			if (_stage.displayState == StageDisplayState.NORMAL) {		
				_stage.displayState = StageDisplayState.FULL_SCREEN;
			}else {	
				_stage.displayState = StageDisplayState.NORMAL;
			}		
		}
		private function onStageResize(e:Event):void{			
			if(_stage.displayState == StageDisplayState.FULL_SCREEN){			
				FullState.fullState = true;	
				changeUI();
				
			}else{		
				FullState.fullState = false;
				resetUI();
			}
		}
		private function changeUI():void{		
			sendNotification(AlbumEvent.CHANGE_UI);
		}	
		private function resetUI():void{		
			sendNotification(AlbumEvent.RESET_UI);
		}
		
	}
}
